package yl.hs.bmipfull.controllers.production;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.production.IProductionLinePortService;
import yl.hs.bmipfull.service.production.IProductionLineService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作用:  生产线信息表控制层<br/>
 * 创建时间: 2021年05月25日 15时50分30秒 <br/>
 *
 * @author 张凯
 */
@Api(value = "生产线信息表Web接口", tags = "生产管理——生产线信息表操作")
@RequestMapping(value = "/api/production")
@RestController
public class ProductionLineController extends ControllerBase {

    @Autowired
    IProductionLineService service;

    @Autowired
    IProductionLinePortService portSvc;
    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "查询生产线信息表", notes = "查询生产线信息表")
    @RequestMapping(value = {"/selectLine/{key}", "/selectLine"}, method = RequestMethod.GET)
    public WebResult queryList(@PathVariable(required = false, name = "key") String key) {
        try {
            return success("查询成功", service.queryList(key));
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "查询生产线信息表", notes = "查询生产线信息表")
    @RequestMapping(value = {"/selectLine/bycatesn/{cateSN}"}, method = RequestMethod.GET)
    public WebResult queryListbyCateSn(@PathVariable(name = "cateSN") String cateSN) {
        try {
            return success("查询成功", service.queryListByProductCate(cateSN));
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "查询生产线信息表", notes = "查询生产线信息表")
    @RequestMapping(value = {"/useLine/{cateSN}"}, method = RequestMethod.GET)
    public WebResult queryUseLine(@PathVariable(required = false, name = "cateSN") String cateSN) {
        try {
            var lines = service.queryListByCate(cateSN);
            var ports = portSvc.queryListPort(null);
            List<Map<String, Object>> newList = new ArrayList<>();
            if (lines.size() > 0) {
                for (var line : lines) {
                    var item = line.toMap();
                    item.put("ports", ports.stream().filter(t -> t.getLineSN().equals(line.getSn())).collect(Collectors.toList()));
                    newList.add(item);
                }
            }
            return success("查询成功", newList);
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询生产线信息表", notes = "分页查询生产线信息表")
    @RequestMapping(value = "/line/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询生产线信息表", notes = "根据生产线信息表编号查询生产线信息表")
    @RequestMapping(value = "/line/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存生产线信息表", notes = "查询/更新生产线信息表信息")
    @RequestMapping(value = "/line", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveProductionLine(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除生产线信息表信息", notes = "根据生产线信息表编号删除系生产线信息表信息")
    @RequestMapping(path = "/line", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "生产线信息表编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }

    @ApiOperation(value = "车辆切换生产线", notes = "根据车辆信息以及生产线信息进行切换")
    @RequestMapping(path = "/switch/{productLine}/{cardata}")
    public WebResult switchProductLine(@ApiParam(value = "生产线编号") @PathVariable String productLine, @ApiParam(value = "车辆信息") @PathVariable String cardata) {
        if (service.Switch(productLine, cardata) > 0) {
            return success("成功");
        } else {
            return fail("失败");
        }
    }
    @ApiOperation(value = "根据生产线编号修改生产线状态", notes = "根据生产线编号修改生产线状态")
    @RequestMapping(value = "/line/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult changeStateBySn(@PathVariable String sn,@PathVariable short state) {
        try {
            Integer result = service.changeStateBySn(sn.split(","), state);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
}